﻿/***************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@gmail.com)

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************************************************/

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BaoJianSoft.Common;
using BaoJianSoft.Web.Extensions.Controls;
using BaoJianSoft.Web.Extensions.Properties;

namespace BaoJianSoft.Web.Extensions.DynamicPages
{
    /// <summary>
    /// Excel detail panel control.
    /// </summary>
    [ParseChildren(true), PersistChildren(false)]
    public sealed class ExcelDetailPanelControl : WebControl, INamingContainer
    {
        private ExcelAttachChildControlsEventHandler onAttachChildControls;

        /// <summary>
        /// Set/get header text of detail panel.
        /// </summary>
        public string HeaderText { get; set; }

        /// <summary>
        /// Set/get true when the background color of panel is transparent.
        /// </summary>
        public bool Transparent { get; set; }

        /// <exclude/>
        public override ControlCollection Controls
        {
            get
            {
                this.EnsureChildControls();
                return base.Controls;
            }
        }

        /// <summary>
        /// Set/get path of ascx template file. The path will be parsed by Server.MapPath("").
        /// </summary>
        public string SkinPath { get; set; }

        /// <summary>
        /// Construct excel detail panel control with attaching child controls callback.
        /// </summary>
        /// <param name="callback">The callback for caller to attach child controls.</param>
        public ExcelDetailPanelControl(ExcelAttachChildControlsEventHandler callback)
        {
            Kit.NotNull(callback, "callback");
            this.onAttachChildControls = callback;
        }

        /// <summary>
        /// Raises the System.Web.UI.Control.Init event.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (!File.Exists(HttpContext.Current.Server.MapPath(this.SkinPath)))
            {
                throw new FileNotFoundException(Resources.SkinFileNotExist, this.SkinPath);
            }

            this.Controls.Clear();
            Container container = new Container() { Transparent = this.Transparent };
            this.Controls.Add(container);

            HtmlGenericControl header = new HtmlGenericControl("h4") { InnerText = this.HeaderText };
            container.Controls.Add(header);
            container.Controls.Add(this.LoadThemedControl());

            this.onAttachChildControls(this);
        }

        /// <exclude/>
        public override void DataBind()
        {
            this.EnsureChildControls();
        }

        /// <exclude/>
        public override Control FindControl(string controlId)
        {
            Control ctrl = base.FindControl(controlId);
            if (ctrl == null && this.Controls.Count == 1)
            {
                ctrl = this.Controls[0].FindControl(controlId);
            }

            if (ctrl == null)
            {
                ctrl = this.Controls[0].Controls[1].Controls[0].FindControl(controlId);
            }

            return ctrl;
        }

        /// <summary>
        /// Loads the skin file from the users current theme
        /// </summary>
        /// <returns></returns>
        private Control LoadThemedControl()
        {
            Control skinTemplate = this.Page.LoadControl(this.SkinPath);
            skinTemplate.ID = Path.GetFileNameWithoutExtension(this.SkinPath);

            Panel panel = new Panel();
            panel.Controls.Add(skinTemplate);

            this.Controls.Add(panel);
            return panel;
        }
    }
}

